package cn.bluemobi.dylan.step.utils.common;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by $Liudan on 2018/8/27 0027.
 */
public class DateUtil {

    /*
     * 将时间戳转换为时间，年月日
     */
    public static String stampToDate(long time){
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date(time);
        res = simpleDateFormat.format(date);
        return res;
    }
    /*
     * 将时间戳转换为时间，年月
     */
    public static String stampToDateMoth(long time){
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM");
        Date date = new Date(time);
        res = simpleDateFormat.format(date);
        return res;
    }

    /*
     * 将时间戳转换为时间，年月日时分秒
     */
    public static String stampToDatehouse(long time){
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date(time);
        res = simpleDateFormat.format(date);
        return res;
    }

    /*
     * 将时间转换为时间戳
     */
    public static long dateToStamp(String s) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = null;
        try {
            date = simpleDateFormat.parse(s);
            if (date==null)return 0;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date.getTime();
    }
    /*
     * 将时间转换为时间戳
     */
    public static long dateToStampDay(String s) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            date = simpleDateFormat.parse(s);
            if (date==null)return 0;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date.getTime();
    }


    /**
     * 将总的训练时间转化为字符串
     *
     * @param totalDuration
     * @return
     */
    public static String formatSecond(int totalDuration) {
        if (totalDuration < 1) return "0";
        StringBuffer time = new StringBuffer();
        int seconds = totalDuration % 60;
        int totalMinutes = totalDuration / 60;
        int minutes = totalMinutes % 60;
        int totalHours = totalMinutes / 60;
        int hours = totalHours % 24;
        int days = totalHours / 24;
        if (days > 0) {
            time.append(days + " 天 " + hours + " 时 " + minutes + " 分 " + seconds + " 秒");
        } else if (hours > 0) {
            time.append(hours + " 时 " + minutes + " 分 " + seconds + " 秒");
        } else if (minutes > 0) {
            if(seconds>0){
                time.append(minutes + " 分 " + seconds + " 秒");
            }else {
                time.append(minutes + " 分钟 ");
            }
        } else {
            time.append(seconds + " 秒");
        }
        return time.toString();
    }

    /**
     * 将总的训练时间转化为字符串
     *
     * @param totalDuration
     * @return
     */
    public static String formatMinutes(int totalDuration) {
        if (totalDuration < 1) return "0";
        StringBuffer time = new StringBuffer();
        int seconds = totalDuration % 60;
        int totalMinutes = totalDuration / 60;
        int minutes = totalMinutes % 60;
        int totalHours = totalMinutes / 60;
        int hours = totalHours % 24;
        int days = totalHours / 24;
        if (days > 0) {
            time.append(days + " 天 " + hours + " 时 " + minutes + " 分 " + seconds + " 秒");
        } else if (hours > 0) {
            time.append(hours + " : " + minutes + " : " + seconds );
        } else if (minutes > 0) {
//            if(seconds>0){
                time.append(minutes + " : " + seconds );
//            }else {
//                time.append(minutes + " : ");
//            }
        } else {
//            time.append(seconds + " 秒");
            time.append(minutes + " : " + seconds );
        }
        return time.toString();
    }

    /**
     * 将毫秒转时分秒
     *
     * @param time
     * @return
     */
    public static String generateTime(long time) {
        int totalSeconds = (int) (time / 1000);
        int seconds = totalSeconds % 60;
        int minutes = (totalSeconds / 60) % 60;
        int hours = totalSeconds / 3600;

//        return hours > 0 ? String.format("%02dh%02dm%02ds", hours, minutes, seconds) : String.format("%02dm%02ds", minutes, seconds);
        return hours > 0 ? String.format("%02d:%02d:%02d", hours, minutes, seconds) : String.format("%02d:%02d", minutes, seconds);
    }

    public static String durationToHours(float time){
        String resultTime;
        if(time<60){
            resultTime = time + "秒";
        }else if(time<60*60){
            float mtime = time/60;
            DecimalFormat decimalFormat=new DecimalFormat("##0.0");
            resultTime = decimalFormat.format(mtime)+"分钟";
        }else {
            float hours = time/60/60;
            DecimalFormat decimalFormat=new DecimalFormat("##0.0");
            resultTime = decimalFormat.format(hours)+"小时";
        }
        return resultTime;
    }

    /**
     * 出生日期计算出年龄
     * @param birthday
     * @return
     */
    public static int getAgeByBirth(Date birthday) {
        int age = 0;
        try {
            Calendar now = Calendar.getInstance();
            now.setTime(new Date());// 当前时间

            Calendar birth = Calendar.getInstance();
            birth.setTime(birthday);

            if (birth.after(now)) {//如果传入的时间，在当前时间的后面，返回0岁
                age = 0;
            } else {
                age = now.get(Calendar.YEAR) - birth.get(Calendar.YEAR);
                if (now.get(Calendar.DAY_OF_YEAR) > birth.get(Calendar.DAY_OF_YEAR)) {
                    age += 1;
                }
            }
            return age;
        } catch (Exception e) {//兼容性更强,异常后返回数据
            return 0;
        }
    }

    public static int getAge(String dateString){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date date =  format.parse(dateString);
            if(date!=null){
                return getAgeByBirth(date);
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 0;
    }

}
